package org.dromara.common.core.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RandomUtil {

    public static Double getDoubleByRange(double minValue,double maxValue){
        double randomValue = minValue + Math.random() * (maxValue - minValue);

        System.out.println("randomValue" + randomValue);
        return MathUtil.getDoubleUp(randomValue);
    }

    /**
     * 把一个正整数随机拆分成count个正整数
     *
     * @param totalNum
     * @param count
     * @return
     */
    public static List<Integer> random(int totalNum, int count) {
        // 创建一个长度的红包数组
        List<Integer> redList = new ArrayList<>();

        int totalMoney = (totalNum);

        /*if (totalMoney < count || totalMoney < 1) {
            return redList; // 返回空的集合
        }*/

        //2. 进行随机分配
        Random rand = new Random();

        int leftMoney = totalMoney;  // 剩余金额
        int leftCount = count;  // 剩余份数
        // 随机分配公式：1 + rand.nextInt(leftMoney / leftCount * 2);
        for (int i = 0; i < count - 1; i++) {
            int money_ = 0;
            if (leftMoney > 0) {
                if ((leftMoney / leftCount * 2) < 1) {
                    money_ = leftMoney;
                } else {
                    money_ = 1 + rand.nextInt(leftMoney / leftCount * 2);
                }

            } else {
                money_ = 0;
            }
            redList.add(money_);
            if (money_ > 0) {
                leftMoney -= money_;
                leftCount--;
            }

        }
        // 把剩余的最后一个放到最后一个包里
        redList.add(leftMoney);
        return redList;
    }


    /**
     * 把一个正整数随机拆分成count个正整数
     *
     * @param totalNum
     * @param count
     * @return
     */
    public static List<Double> randomDoubleByTotalAndCount(double totalNum, int count, double minValue, double maxValue) {
        // 创建一个长度的红包数组
        List<Double> redList = new ArrayList<>();
//        double totalMoney = (totalNum);

        /*if (totalMoney < count || totalMoney < 1) {
            return redList; // 返回空的集合
        }*/

        //2. 进行随机分配
//        Random rand = new Random();

        double leftMoney = totalNum;  // 剩余金额
        int leftCount = count;  // 剩余份数
        // 随机分配公式：1 + rand.nextInt(leftMoney / leftCount * 2);
        for (int i = 0; i < count - 1; i++) {
            double money_ = 0;
            if (leftMoney > 0) {
                if ((leftMoney / leftCount * 2) < 1) {
                    money_ = leftMoney;
                } else {
                    money_ =  minValue + Math.random() * (maxValue - minValue);  //1 + rand.nextDouble(leftMoney / leftCount * 2);
                }

            } else {
                money_ = 0;
            }
            money_ = MathUtil.getFloat(money_);
            redList.add(money_);
            if (money_ > 0) {
                leftMoney -= money_;
                leftCount--;
            }

        }
        // 把剩余的最后一个放到最后一个包里
        redList.add(MathUtil.getFloat(leftMoney));
        return redList;
    }


    /**
     * 把一个正整数随机拆分成count个正整数
     *
     * @param totalNum
     * @param count
     * @return
     */
    public static List<Integer> randomIntegerByTotalAndCount(int totalNum, int count, int minValue, int maxValue) {
        // 创建一个长度的红包数组
        List<Integer> redList = new ArrayList<>();
//        double totalMoney = (totalNum);

        /*if (totalMoney < count || totalMoney < 1) {
            return redList; // 返回空的集合
        }*/

        //2. 进行随机分配
        Random rand = new Random();

        int leftMoney = totalNum;  // 剩余金额
        int leftCount = count;  // 剩余份数
        // 随机分配公式：1 + rand.nextInt(leftMoney / leftCount * 2);
        for (int i = 0; i < count - 1; i++) {
            int money_ = 0;
            if (leftMoney > 0) {
                if ((leftMoney / leftCount * 2) < 1) {
                    money_ = leftMoney;
                } else {
//                    rand.nextInt(leftMoney / leftCount * 2)
                    money_ =  minValue + (int)(Math.random() * (maxValue - minValue));  //1 + rand.nextDouble(leftMoney / leftCount * 2);
//                    money_ =  minValue + rand.nextInt(leftMoney / leftCount * 2);
                }

            } else {
                money_ = 0;
            }
            redList.add(money_);
            if (money_ > 0) {
                leftMoney -= money_;
                leftCount--;
            }

        }
        // 把剩余的最后一个放到最后一个包里
        redList.add(leftMoney);
        return redList;
    }


    public static void main(String[] args) {
//        int random = getRandom(3000,10000);
//        System.out.println("random : = " + random);
//        double dd = getDoubleByRange(3000,10000);
//        System.out.println("dd : =" + dd);

        List<Double> randomList = randomDoubleByTotalAndCount(5000.00,6,500,1200);
        System.out.println("dd : =" + randomList.toString());


        List<Integer> randomList1 = randomIntegerByTotalAndCount(5000,6,500,1200);
        System.out.println("dd 111 : =" + randomList1.toString());
    }
}
